<template>
  <div :style="{height:height,width:width}" class="chart" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

export default {
  props: {
    width: {
      type: String,
      required: false,
      default: (window.innerWidth - 50) + 'px'
    },
    height: {
      type: String,
      required: false,
      default: '300px'
    },
    autoResize: {
      type: Boolean,
      required: false,
      default: true
    },
    chartData: {
      type: Object,
      required: true,
      default: () => {}
      /* {'y': [{'label','pv','data':[800, 400, 8000],'extend': ''},{'label','ip','data':[1000, 2000, 3000],'extend': ''}], 'x': ['2020-05-13', '2020-05-19', '2020-05-26'] }
      */
    }
  },
  data() {
    return {
      chart: null,
      sidebarElm: null
      /* chartData: {
        visitsData: [1000, 2000, 3000],
        ipData: [800, 400, 8000],
        legendData: ['pv','ip']
      },
      weekDays: ['2020-05-13', '2020-05-19', '2020-05-26']*/
    }
  },
  watch: {
    chartData() {
      if (this.chartData.x) {
        this.initChart()
      }
    }
  },
  mounted() {
    if (this.autoResize) {
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHandler)
    }
    // 监听侧边栏的变化
    this.sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    this.sidebarElm && this.sidebarElm.addEventListener('transitionend', this.sidebarResizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHandler)
    }
    this.sidebarElm && this.sidebarElm.removeEventListener('transitionend', this.sidebarResizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    sidebarResizeHandler(e) {
      if (e.propertyName === 'width') {
        this.__resizeHandler()
      }
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    formatSeries(chartData) {
      const series = []
      chartData.y.forEach(tmpy => {
        const extend = tmpy.extend
        const tmpseies = {
          name: tmpy.label,
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: tmpy.data,
          animationDuration: extend.animationDuration || 6000
        }
        series.push(tmpseies)
      })
      return series
    },
    setOptions(chartData) {
      this.chart.setOption({
        xAxis: {
          type: 'category',
          data: this.chartData.x,
          axisTick: {
            alignWithLabel: true
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          },
          padding: [5, 10]
        },
        yAxis: {
          type: 'value',
          axisTick: {
            show: false
          }
        },
        series: this.formatSeries(chartData)
      })
    }
  }
}
</script>
